#!/bin/bash

Upgrade_PHP_7()
{

    echo -e "\033[34mStart install php-${php_version}\033[0m"
    tar zxf php-${php_version}.tar.gz
    cd php-${php_version}
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-opcache --with-xsl
    make ZEND_EXTRA_LIBS='-liconv'
    make install
    ln -sf /usr/local/php/bin/php /usr/bin/php
    ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
    ln -sf /usr/local/php/bin/pear /usr/bin/pear
    ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
    ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

    echo "Copy new php configure file."
    mkdir -p /usr/local/php/etc
    \cp php.ini-production /usr/local/php/etc/php.ini
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/default_socket_timeout = 60/default_socket_timeout = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 4096M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    echo "Write Opcache to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
[Zend Opcache]
zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
EOF
    pear config-set php_ini /usr/local/php/etc/php.ini
    pecl config-set php_ini /usr/local/php/etc/php.ini
    curl -sS --connect-timeout 30 -m 60 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    if [ $? -eq 0 ]; then
        echo "Composer install successfully."
    else
        if [ -s /usr/local/php/bin/php ]; then
            wget --prefer-family=IPv4 --no-check-certificate -T 120 -t3 ${Download_Mirror}/composer/composer.phar -O /usr/local/bin/composer
            if [ $? -eq 0 ]; then
                echo "Composer install successfully."
            else
                echo "Composer install failed!"
            fi
            chmod +x /usr/local/bin/composer
        fi
    fi
    echo "Creating new php-fpm configure file......"
    cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /run/php-cgi.sock
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
    echo "Copy php-fpm init.d file......"
    \cp ${cur_dir}/src/php-${php_version}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    moodle start
    if [[ -s /usr/local/php/sbin/php-fpm && -s /etc/init.d/php-fpm && -s /usr/local/php/etc/php.ini && -s /usr/local/php/bin/php ]]; then
        echo -e "\033[32m======== upgrade php completed ======\033[0m"
     else
        echo -e "\033[31m======== upgrade php failed ======\033[0m"
        echo -e "\033[31mupgrade php log: /root/upgrade_lnmp_php.log\033[0m"
        echo "You mail upgrade_lnmp_php.log to hongm@sina.com for help."
    fi
}

Upgrade_PHP()
{

    php_version=""
    Cur_PHP_Version=$( /usr/local/php/bin/php -r 'echo PHP_VERSION;' )
    zend_ext_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/"
    echo "Current PHP Version:${Cur_PHP_Version}"
    echo "You can get version number from http://www.php.net/"
    echo "Please enter a PHP Version you want."
    read -p "(example: ${PHP_Version} ): " php_version
    if [ "${php_version}" = "" ]; then
        echo "Error: You must enter a corrent php version!!"
        exit 1
    fi

    Press_Start
    cd ${cur_dir}/src
    if [ -s php-${php_version}.tar.gz ]; then
        echo "php-${php_version}.tar.gz [found]"
     else
        echo "Error: php-$php_version.tar.gz not found!!!download now..."
        cd ${cur_dir}/src
        wget -c ${Download_Mirror}/php/php-${php_version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download php-${php_version}.tar.gz successfully!"
         else
            wget -c http://www.php.net/distributions/php-${php_version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download php-${php_version}.tar.gz successfully!"
             else
                wget -c http://museum.php.net/php5/php-${php_version}.tar.gz
                if [ $? -eq 0 ]; then
                    echo "Download php-${php_version}.tar.gz successfully!"
                 else
                    echo "You enter PHP Version was:"${php_version}
                    echo -e "\033[31mError! You entered a wrong version number, please check!\033[0m"
                    sleep 5
                    exit 1
                fi
            fi
        fi
    fi

    moodle stop
    mv /usr/local/php /usr/local/php-${Cur_PHP_Version}
    mv /etc/init.d/php-fpm /usr/local/php-${Cur_PHP_Version}
    if echo "${php_version}" | grep -Eqi '^7..';then
        Upgrade_PHP_7
    fi
    /etc/init.d/php-fpm restart
    rm -fr ${cur_dir}/src/php-${php_version}
}
